﻿<UserControl 
	x:Class="MVVMtutorialAdvanced.Books_UC"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	xmlns:WindowsControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
	xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
	xmlns:dataToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
	xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
	xmlns:RibaConverter="clr-namespace:Riba.Tools.Converter;assembly=RibaTools"
	xmlns:RibaControls="clr-namespace:Riba.Tools.Controls;assembly=RibaTools"
	mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="600"
	DataContext="{Binding Source={StaticResource App_ViewModelLocator}, Converter={StaticResource App_ViewModelNameToViewModelConverter}, ConverterParameter=MVVMtutorialAdvanced.Books_VM}"
>
	<UserControl.Resources>
		<Style TargetType="RibaControls:TextBoxWithMaxLengthFromStringLengthAttribute">
			<Setter Property="RibaControls:TextBoxBindingHelper.UpdateSourceOnChange" Value="True" />
		</Style>

		<DataTemplate x:Key="countryComboBoxTemplate">
			<StackPanel Orientation="Horizontal">
				<Image Source="{Binding Id, Converter={StaticResource App_CountryIdToImageConverter}}" Margin="4, 0, 10, 0" Height="20" />
				<TextBlock
					Width="150"
					Text="{Binding Path=Alias}"
					VerticalAlignment="Center"
				/>
			</StackPanel>
		</DataTemplate>

		<RibaConverter:InvertBoolConverter x:Key="InvertBoolConv" />
		<RibaConverter:ByteArrayToImageConverter x:Key="ByteArrayToImageConv" />
	</UserControl.Resources>	

	<Grid Grid.Row="1" Background="White">
		<Grid.ColumnDefinitions>
			<ColumnDefinition MinWidth="100" MaxWidth="500" Width="250" />
			<ColumnDefinition Width="*" />
		</Grid.ColumnDefinitions>

		<Grid HorizontalAlignment="Stretch" Margin="1, 0, 4, 0">
			<Grid.RowDefinitions>
				<RowDefinition Height="24" />
				<RowDefinition Height="*" />
				<RowDefinition Height="24" />
			</Grid.RowDefinitions>

			<Border Background="AliceBlue" Padding="2">
				<TextBlock Text="Bücher (5 pro Seite)" FontWeight="Bold" VerticalAlignment="Center" />
			</Border>
			
			<Grid Grid.Row="1">
				<Grid.RowDefinitions>
					<RowDefinition Height="*" />
					<RowDefinition Height="24" />
				</Grid.RowDefinitions>

				<!--SelectedItem="{Binding CurrentEntity, Mode=TwoWay}"-->
				<sdk:DataGrid
					ItemsSource="{Binding EntityList}"				
					IsReadOnly="True"
					AutoGenerateColumns="True" 
					RowDetailsVisibilityMode="VisibleWhenSelected"
					IsEnabled="{Binding IsDirty, Converter={StaticResource InvertBoolConv}}"
				/>

				<sdk:DataPager Source="{Binding EntityList}" IsEnabled="{Binding IsDirty, Converter={StaticResource InvertBoolConv}}" Grid.Row="1" />
			</Grid>
			
			<StackPanel Orientation="Horizontal" Background="AliceBlue" Grid.Row="2">
				<Button Content="Einfügen" Command="{Binding CommandInsertEntity}" />
				<Button Content="Löschen" Command="{Binding CommandDeleteEntity}" />
				<Button Content="Erneuern" Command="{Binding CommandRefreshEntities}" />
			</StackPanel>
		</Grid>

		<WindowsControls:GridSplitter Width="3" Background="#FFB4B9D1" />

		<Grid Visibility="{Binding DetailsVisibility}" Grid.Column="1">
			<Grid.RowDefinitions>
				<RowDefinition Height="24" />
				<RowDefinition Height="*" />
				<RowDefinition Height="24" />
			</Grid.RowDefinitions>

			<Border Background="AliceBlue" Padding="2">
				<TextBlock Text="Details" FontWeight="Bold" VerticalAlignment="Center" />
			</Border>

			<ScrollViewer Grid.Row="1">
				<Grid>
					<Grid.RowDefinitions>
						<RowDefinition Height="30" />
						<RowDefinition Height="30" />
						<RowDefinition Height="30" />
						<RowDefinition Height="30" />
						<RowDefinition Height="30" />
						<RowDefinition Height="30" />
						<RowDefinition Height="84" />
						<RowDefinition Height="*" />
					</Grid.RowDefinitions>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}">
						<TextBlock Text="{Binding CurrentEntity.Id, Mode=TwoWay}" />
					</dataToolkit:DataField>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}" Grid.Row="1">
						<RibaControls:TextBoxWithMaxLengthFromStringLengthAttribute Text="{Binding CurrentEntity.Title, Mode=TwoWay}" />
					</dataToolkit:DataField>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}" Grid.Row="2">
						<RibaControls:TextBoxNumeric Text="{Binding CurrentEntity.Price, Mode=TwoWay}" IsDecimal="True" MaxLength="10" />
					</dataToolkit:DataField>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}" Grid.Row="3">
						<CheckBox IsChecked="{Binding CurrentEntity.IsOutOfStock, Mode=TwoWay}" />
					</dataToolkit:DataField>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}" Grid.Row="4">
						<WindowsControls:DatePicker SelectedDate="{Binding CurrentEntity.Release, Mode=TwoWay}" SelectedDateFormat="Long" Language="de" />
					</dataToolkit:DataField>

					<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}" Grid.Row="5">
						<RibaControls:ComboBoxEx 
							ItemsSource="{Binding Countries}"
							SelectedValueProper="{Binding CurrentEntity.CountryId, Mode=TwoWay}"
							SelectedValuePath="Id"
							ItemTemplate="{StaticResource countryComboBoxTemplate}"
						/>
					</dataToolkit:DataField>

					<StackPanel Grid.Row="6" Orientation="Horizontal">
						<dataToolkit:DataField LabelStyle="{StaticResource LabelStyle}">
							<Image Width="80" Height="80" Source="{Binding CurrentEntity.Cover, Converter={StaticResource ByteArrayToImageConv}}" />
						</dataToolkit:DataField>
						<Button Content="Laden ..." Command="{Binding CommandLoadImage}" Height="24" />
					</StackPanel>

					<StackPanel Grid.Row="7" Margin="2">
						<TextBlock Text="Autoren" />

						<StackPanel>
							<sdk:DataGrid
								ItemsSource="{Binding CurrentEntity.BookAuthorDtos}"
								SelectedItem="{Binding CurrentBookAuthor, Mode=TwoWay}"
								AutoGenerateColumns="True" 
								RowDetailsVisibilityMode="VisibleWhenSelected"
								IsReadOnly="True"
								Height="185"
							/>

							<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0, 10, 0, 0">
								<Button Content="Autor hinzufügen ..." Command="{Binding CommandAddAuthor}" />
								<Button Content="Autor löschen" Command="{Binding CommandDeleteAuthor}" />
							</StackPanel>
						</StackPanel>
					</StackPanel>
				</Grid>
			</ScrollViewer>

			<StackPanel Orientation="Horizontal" Background="AliceBlue" Grid.Row="2">
				<Button Content="Abbrechen" Command="{Binding CommandRevertEntity}" />
				<Button Content="Speichern" Command="{Binding CommandSaveEntity}" />
			</StackPanel>
		</Grid>

		<toolkit:BusyIndicator
			BusyContent="Bitte warten ..." 
			IsBusy="{Binding IsBusy}"
			Grid.RowSpan="8"
			Grid.ColumnSpan="2"
		/>
	</Grid>
</UserControl>